﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Board.Models
{
    public class Bed
    {
        private bool _exist = false;

        private List<WebUser> _accessibleUsers = null; 

        public static string ALL_BEDS = "All_Beds";

        public string SiteId { get; set; }

        public string UnitId { get; set; }

        public string BedId { get; set; }

        public long BoardId { get; set; }

        public List<WebUser> AccessibleUsers
        {
            get
            {
                if (_accessibleUsers == null)
                {
                    _accessibleUsers = new List<WebUser>();
                    if (_exist)
                    {

                        using (DbEntities db = new DbEntities())
                        {
                            _accessibleUsers.AddRange((from u in db.t_UserProfile
                                                      join p in db.t_UnitBedPermission on u.UserId equals p.UserId
                                                      where
                                                          ((p.SiteID.Equals(SiteId) || p.SiteID.Equals(Site.ALL_SITES)) &&
                                                           (p.UnitID.Equals(UnitId) || p.UnitID.Equals(Unit.ALL_UNITS)) &&
                                                           (p.Bed.Equals(BedId) || p.Bed.Equals(Bed.ALL_BEDS))) 
                                                      orderby u.FirstName, u.LastName
                                                       select u).ToList().Select(u => new WebUser(u, new HashSet<t_webpages_Roles>(u.webpages_Roles))));

                        }

                    }
                }
                return _accessibleUsers;
            }
        }

        public string BedStatus { get; set; }

        public string BedType { get; set; }

        public static string BED_TYPE_KARDEX_MOM = "KardexMom";
        public static string BED_TYPE_KARDEX_BABY = "KardexBaby";

        public static string BED_STATUS_ACTIVE = "A";
        public static string BED_STATUS_PENDING = "P";
        public static string BED_STATUS_CLOSED = "C";
        public static string BED_STATUS_ACTIVE_DESCRIPTION = "Active";
        public static string BED_STATUS_PENDING_DESCRIPTION = "Pending";
        public static string BED_STATUS_CLOSED_DESCRIPTION = "Closed";

        public static List<string> BedTypeList
        {
            get
            {
                return new List<string>() { BED_TYPE_KARDEX_MOM, BED_TYPE_KARDEX_BABY };
            }
        }

        public static List<string> BedStatusList
        {
            get
            {
                return new List<string>(){BED_STATUS_ACTIVE, BED_STATUS_PENDING, BED_STATUS_CLOSED};
            }
        } 

        public static List<string> BedStatusDescriptionList
        {
            get
            {
                return new List<string>() { BED_STATUS_ACTIVE_DESCRIPTION, BED_STATUS_PENDING_DESCRIPTION, BED_STATUS_CLOSED_DESCRIPTION };
            }
        } 

        public bool Exist
        {
            get
            {
                if (!_exist && !string.IsNullOrEmpty(SiteId + UnitId + BedId))
                {
                    using (DbEntities db = new DbEntities())
                    {
                        _exist = (from u in db.t_AllUnitBed
                                  where u.SiteID.Equals(SiteId) && u.UnitID.Equals(UnitId) && u.Bed.Equals(BedId)
                                  select u).Any();
                    }
                }
                return _exist;
            }
        }

        public Bed(string siteId, string unitId, string bed)
        {
            using (DbEntities db = new DbEntities())
            {
                var beds = from u in db.t_AllUnitBed
                           where u.SiteID.Equals(siteId) && u.UnitID.Equals(unitId) && u.Bed.Equals(bed)
                           select u;
                if (beds.Any())
                {
                    SiteId = siteId;
                    UnitId = unitId;
                    BedId = bed;
                    BoardId = beds.FirstOrDefault().BoardId;
                    BedStatus = beds.FirstOrDefault().BedStatus;
                    BedType = beds.FirstOrDefault().BedType;
                    _exist = true;
                }
                else
                {
                    _exist = false;
                }
            }
        }
        public Bed()
        {
        }

        public Bed(long boardId)
        {
            using (DbEntities db = new DbEntities())
            {
                var beds = from u in db.t_AllUnitBed
                           where u.BoardId == boardId
                           select u;
                if (beds.Any())
                {
                    var theBed = beds.FirstOrDefault();
                    SiteId = theBed.SiteID;
                    UnitId = theBed.UnitID;
                    BedId = theBed.Bed;
                    BoardId = theBed.BoardId;
                    BedStatus = theBed.BedStatus;
                    BedType = theBed.BedType;
                    _exist = true;
                }
                else
                {
                    _exist = false;
                }
            }
        }



        public void Save()
        {
            if (BoardId > 0)
            {
                using (DbEntities db = new DbEntities())
                {
                    var beds = from b in db.t_AllUnitBed
                               where b.BoardId == BoardId
                               select b;
                    if (beds.Any())
                    {
                        var bed = beds.FirstOrDefault();
                        if (WebLog.LogValueChanged(db, "AllUnitBed", "BedStatus", bed.BedStatus,
                                                  BedStatus))
                        {
                            bed.BedStatus = BedStatus;
                        }
                        string theBedType = string.IsNullOrEmpty(BedType) ? "" : BedType.Trim().Trim(',');
                        if (!string.IsNullOrEmpty(theBedType))
                        {
                            var tempList = theBedType.Split('-');
                            Array.Sort(tempList);
                            theBedType = string.Join("-", tempList);
                        }
                        if (WebLog.LogValueChanged(db, "AllUnitBed", "BedType", bed.BedType,
                            theBedType))
                        {
                            bed.BedType = theBedType;
                        }
                        db.SaveChanges();
                    }

                }
            }
        }

    }
}